package app.server.project.cloud.random.db.entity;

import app.server.project.core.constants.Def;
import app.server.project.core.syndb.SynDBMgr;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "user_device")
@TableName("user_device")
@Comment(value = "用户设备信息表")
public class UserDeviceEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @TableField("user_id")
    @Comment("用户id")
    @Column(name = "user_id")
    private long userId;

    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;

    @TableField("device_id")
    @Comment("房间名称")
    @Column(name = "device_id")
    private long deviceId;


    @TableField("room_id")
    @Comment("房间id")
    @Column(name = "room_id")
    private long roomId;

    @TableField("is_del")
    @Comment("删除")
    @Column(name = "is_del")
    private long isDel;


    @TableField("name")
    @Comment("设备名称")
    @Column(name = "name")
    private String name;



    @TableField("dev_type_id")
    @Comment("设备类型id")
    @Column(name = "dev_type_id")
    private int devTypeId;


    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserDeviceEntity::getCreateTime, this);
    }

    public void setDevTypeId(int devTypeId) {
        this.devTypeId = devTypeId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserDeviceEntity::getDevTypeId, this);
    }

    public void setName(String name) {
        this.name = name;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserDeviceEntity::getName, this);
    }


    public void setUserId(long userId) {
        this.userId = userId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserDeviceEntity::getUserId, this);
    }

    public void setDeviceId(long deviceId) {
        this.deviceId = deviceId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserDeviceEntity::getDeviceId, this);
    }

    public void setIsDel(long isDel) {
        this.isDel = isDel;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserDeviceEntity::getIsDel, this);
    }

    public void setRoomId(long roomId) {
        this.roomId = roomId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserDeviceEntity::getRoomId, this);
    }

    public static UserDeviceEntity of(long id, long userId, long deviceId,int devTypeId){
        var ret=new UserDeviceEntity();
        ret.setSynDb(true);
        ret.setId(id);
        ret.setIsDel(Def.FALSE);
        ret.setRoomId(Def.Zero);
        ret.setDeviceId(deviceId);
        ret.setUserId(userId);
        ret.setDevTypeId(devTypeId);
        ret.setCreateTime(LocalDateTime.now());

        return ret;
    }

}
